//
//  RealtimeModel.swift
//  MeasureMould
//
//  Created by iOS on 2021/4/1.
//

import Foundation

class RealtimeModel:NSObject{
    var rt_lead:UInt8 = 0
    /// 数据编号
    var rt_num:String = ""
    /// 经度
    var rt_longitude:Double = 0
    /// 纬度
    var rt_laitude:Double = 0
    /// 面积
    var rt_area:Int = 0
    var rt_speed:UInt8 = 0

    var lat_d:Double = 0
    var lat_m:Double = 0
    var lat_s:Double = 0

    var lon_d:Double = 0
    var lon_m:Double = 0
    var lon_s:Double = 0

    /// 实时数据包
    /// - Parameter data: 数据包
    func realtimeData(formData data:Data){
        let byteArr = data
        if byteArr.count > 0 {
            for (index,value) in byteArr.enumerated() {
                switch index {
                    case 0:
                        break
                    default:
                        break
                }
            }
        }
//        let long_d = Double(byteArr[5])
//        let long_m = Double(byteArr[6]) / 60
//        let lon_temp = UInt(byteArr[7]) << 8
//        let long_s = Double(lon_temp) // 60 / 60
//        let long_s1 = Double(byteArr[8]) // 60 / 60 / 1000
//        let long_s2 = Double(byteArr[9]) // 60 / 60 / 1000000
//        let lon_sss = (long_s + long_s1 + long_s2) / 3600 / 100
//        let long = long_d + long_m + lon_sss//long_s + long_s1 + long_s2
//
//        let lat_d = Double(byteArr[10])
//        let lat_m = Double(byteArr[11]) / 60
//        let lat_temp = UInt(byteArr[12]) << 8
//        let lat_s = Double(lat_temp) // 60 / 60
//        let lat_s1 = Double(byteArr[13]) // 60 / 60 / 1000
//        let lat_s2 = Double(byteArr[14]) // 60 / 60 / 1000000
//        let lat_sss = (lat_s + lat_s1 + lat_s2) / 3600 / 100
//        let lat = lat_d + lat_m + lat_sss//lat_s + lat_s1 + lat_s2
//
//        self.lon_d = long_d
//        self.lon_m = long_m
//        self.lon_s = lon_sss
//
//        self.lat_d = lat_d
//        self.lat_m = lat_m
//        self.lat_s = lat_sss

        let rt_lead = byteArr[0]
        let rt_num = String(format: "\(byteArr[1]).\(byteArr[2])\(byteArr[3])\(byteArr[4])")
//        let rt_longitude = String(format: "\(byteArr[5]).\(byteArr[6])\(byteArr[7])\(byteArr[8])\(byteArr[9])")
//        let rt_latitude = String(format: "\(byteArr[10]).\(byteArr[11])\(byteArr[12])\(byteArr[13])\(byteArr[14])")
//        let rt_area = String(format: "\(byteArr[15]).\(byteArr[16])\(byteArr[17])\(byteArr[18])")
        self.rt_lead = rt_lead
        self.rt_num = rt_num
//        let amapcoord = AMapCoordinateConvert(CLLocationCoordinate2D(latitude: lat, longitude: long), AMapCoordinateType.GPS)
//        self.rt_longitude = amapcoord.longitude
//        self.rt_laitude = amapcoord.latitude
//        self.rt_area = rt_area

        self.rt_speed = byteArr[25] + byteArr[26]

        }

    func boolthRelDataWithHex(_ hex:String){
        let oc_hex = hex as NSString
        let lon_d = oc_hex.substring(with: NSRange(location: 10, length: 2))
        let dd_lon = Int(lon_d, radix: 16)!
        let lon_m = oc_hex.substring(with: NSRange(location: 12, length: 2))
        let mm_lon = Int(lon_m, radix: 16)!
        let lon_s = oc_hex.substring(with: NSRange(location: 14, length: 4))
        let s_lon = Int(lon_s, radix: 16)!
        let lon_ss = oc_hex.substring(with: NSRange(location: 18, length: 2))
        let ss_lon = Int(lon_ss, radix: 16)!

        let lat_d = oc_hex.substring(with: NSRange(location: 20, length: 2))
        let dd_lat = Int(lat_d, radix: 16)!
        let lat_m = oc_hex.substring(with: NSRange(location: 22, length: 2))
        let mm_lat = Int(lat_m, radix: 16)!
        let lat_s = oc_hex.substring(with: NSRange(location: 24, length: 4))
        let s_lat = Int(lat_s, radix: 16)!
        let lat_ss = oc_hex.substring(with: NSRange(location: 28, length: 2))
        let ss_lat = Int(lat_ss, radix: 16)!

        let lon = LocationConverter.getLatLon(dd_lon, mm: mm_lon, ss: s_lon, sss: ss_lon)
        let lat = LocationConverter.getLatLon(dd_lat, mm: mm_lat, ss: s_lat, sss: ss_lat)
//        self.pro_latitude = lat
//        self.pro_longitude = lon
        let coord2D = CLLocationCoordinate2D(latitude: lat, longitude: lon)
        let amapcoord = AMapCoordinateConvert(coord2D, AMapCoordinateType.GPS)
        self.rt_longitude = amapcoord.longitude
        self.rt_laitude = amapcoord.latitude

        let area_hex = oc_hex.substring(with: NSRange(location: 30, length: 8))
        let area = Int(area_hex, radix: 16)
        self.rt_area = area!

    }

}
